<div class="wrapper">
    <div class="treeShowHideButton">
        <label id="btnShow" title="显示搜索" style="display:none;">︾</label>
        <label id="btnHide" title="隐藏搜索">︽</label>
    </div>
    <div class="treeSearchInput" id="search">
        <label for="keyword">关键字：</label><input type="text" class="empty" id="keyword" maxlength="50"/>
        <button class="btn" id="btn" onclick="searchNode()"> 搜索</button>
    </div>
    <div class="treeExpandCollapse">
        <a href="javascript:" onclick=" Page.AreaTree.showAllNode();">展开</a> /
        <a href="javascript:" onclick=" Page.AreaTree.hideAllNode();">折叠</a>
    </div>
    <div id="areaTree" class="ztree" style="padding: 10px 20px;"></div>

</div>

<script>
    $.namespace('Page.AreaTree');

    Page.AreaTree = function () {
        var isFirst = true, setting = {
            async: {
                enable: true,
                url: "${base}/admin/sys/area/getTree",
                autoParam: ["id", "realName=n", "level=lv"],
            },
            check: {enable: "" == "true", nocheckInherit: true},
            view: {selectedMulti: false, dblClickExpand: false},
            callback: {
                onClick: function (event, treeId, treeNode) {
                    Page.AreaTree.tree.expandNode(treeNode);
                },
                onAsyncSuccess: function (event, treeId, treeNode, msg) {
                    if (isFirst) {
                        var expendNodes = Page.AreaTree.tree.getNodesByParam("level", 0)
                        for (var i = 0; i < expendNodes.length; i++) {
                            Page.AreaTree.tree.expandNode(expendNodes[i], true, false, false);
                        }
                        isFirst = false;
                    }
                },
                onDblClick: function () {

                }
            }
        }, tree;
        return {
            init: function () {
                Page.AreaTree.tree = $.fn.zTree.init($("#areaTree"), setting)
            },

            hideAllNode: function () {
                Page.AreaTree.tree.expandAll(false);
            },
            showAllNode: function () {
                Page.AreaTree.tree.expandAll(true);
            },

            selectNode: function () {
                  var nodes =   Page.AreaTree.tree.getSelectedNodes();
                  if(nodes){
                      var data = {};
                      $.each(nodes[0],function (key,value) {
                          if(key == 'attribute'){
                              $.extend(data,value)
                          }else {
                              data[key] = value;
                          }
                      })
                      Bolt.bringBack(data);
                  }

            }

        }
    }();

    $(document).ready(function () {
        Page.AreaTree.init();
    });
</script>